<template>
  <div class="p-6px">
    <el-card class="rounded-md" shadow="never">
      <!-- 搜索条件 -->
      <el-form v-show="showSearch" :inline="true" @submit.prevent>
        <el-form-item label="分销员名称" prop="name">
          <el-input placeholder="请输入分销员名称" v-model="searchParams.name" clearable style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" plain v-throttle="handleSearch">搜索</el-button>
          <el-button type="danger" icon="refresh" plain v-debounce="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格头部按钮 -->
      <el-row :gutter="10">
        <el-col :span="1.5" v-auth="['plugin:colecron:cron:add']">
          <!-- <el-button type="primary" icon="plus" plain @click="handleAdd()">新增</el-button> -->
        </el-col>
        <KoiToolbar v-model:showSearch="showSearch" @refresh-table="handleListPage"></KoiToolbar>
      </el-row>

      <br />
      <!-- 数据表格 -->
      <el-table v-loading="loading" v-adaptive border :data="tableList" empty-text="暂时没有数据哟">
        <el-table-column label="序号" prop="id" align="center" type="index"></el-table-column>
        <el-table-column label="分销员名称" prop="user.nickname" width="100px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="分销等级" prop="level.name" width="100px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="下级人员数" prop="children_num" width="100px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="提成单量" prop="agent_order_num" width="100px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column
          label="提成订单金额"
          prop="agent_order_amount"
          width="130px"
          :show-overflow-tooltip="true"
          align="center"
        >
        </el-table-column>

        <el-table-column
          label="提成实际支付金额"
          prop="agent_pay_amount"
          width="150px"
          :show-overflow-tooltip="true"
          align="center"
        >
        </el-table-column>

        <el-table-column label="总佣金数" prop="total_bonus" width="100px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="已提款金额" prop="draw_amount" width="130px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="已提款次数" prop="draw_num" width="130px" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="剩余可提款金额" width="150px" prop="residue_amount" :show-overflow-tooltip="true" align="center">
        </el-table-column>

        <el-table-column label="绑定时间" prop="bind_time" :show-overflow-tooltip="true" align="center"> </el-table-column>
        <el-table-column label="状态" prop="status_text" align="center" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column
          label="操作"
          align="left"
          width="350"
          fixed="right"
          v-auth="['system:role:update', 'system:role:delete']"
        >
          <template #default="{ row }">
            <el-button type="warning" v-if="row.status == 1" size="small" plain v-auth="['system:role:update']">
              修改上级
            </el-button>
            <el-button type="success" v-if="row.status == 0" size="small" plain v-auth="['system:role:update']">
              审核通过
            </el-button>
            <el-button type="warning" v-if="row.status == 0" size="small" plain v-auth="['system:role:update']">
              审核拒绝
            </el-button>
            <el-button type="danger" v-if="row.status == 1" size="small" plain v-auth="['system:role:update']"> 禁用 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <!-- 分页 -->
      <el-pagination
        background
        v-model:current-page="searchParams.pageNo"
        v-model:page-size="searchParams.pageSize"
        v-show="total > 0"
        :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleListPage"
        @current-change="handleListPage"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts" name="coleAppVersionPage">
import { listPage } from "@/plugin/agent/api/user/index.ts";
import { koiNoticeError } from "@/utils/koi.ts";
import { onMounted, ref } from "vue";

// 数据表格加载页面动画
const loading = ref(false);
/** 是否显示搜索表单 */
const showSearch = ref<boolean>(true); // 默认显示搜索条件
// 数据表格数据
const tableList = ref<any>([]);
const total = ref<number>(0);

// 查询参数
const searchParams = ref({
  pageNo: 1,
  pageSize: 10,
  name: ""
});

// 重置搜索参数
const resetSearchParams = () => {
  searchParams.value = {
    pageNo: 1,
    pageSize: 10,
    name: ""
  };
};

/** 搜索 */
const handleSearch = () => {
  searchParams.value.pageNo = 1;
  handleListPage();
};

/** 重置 */
const resetSearch = () => {
  resetSearchParams();
  handleListPage();
};

/** @current-change：点击分页组件页码发生变化：例如：切换第2、3页 OR 上一页 AND 下一页 OR 跳转某一页 */
/** @size-change：点击分页组件下拉选中条数发生变化：例如：选择10条/页、20条/页等 */
// 分页查询，@current-change AND @size-change都会触发分页，调用后端分页接口
/** 数据表格 */
const handleListPage = async () => {
  try {
    loading.value = true;
    tableList.value = []; // 重置表格数据
    const res: any = await listPage(searchParams.value);
    tableList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  } catch (error) {
    console.log(error);
    koiNoticeError("数据查询失败，请刷新重试");
  }
};

onMounted(() => {
  // 获取数据表格数据
  handleListPage();
});
</script>

<style lang="scss" scoped></style>
